<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
	<title>Getting Started Part2</title>
  <link rel="stylesheet" type="text/css" href="documentation.css" />
</head>
<body>

<h1 id="second-tutorial">Second tutorial</h1>

<p>For this tutorial we presume that you have completed the first tutorial, since it builds on the code produced from that.</p>

<h2 id="negotiating-the-content-type">Negotiating the Content-Type</h2>

<p>Most of the time a web application will emit HTML. That's the fabric of which the web is made. Once in a while though, we might have to generate other types of responses. In these cases, we clearly don't want the output to get wrapped in the regular document rendering. For that we need an abnormal response. We have already seen one kind of abnormal response, with the <code>k_NotFound</code>, but this time we need a more specific kind. We'll extend the contact component with the capacity to render as a <a href="http://en.wikipedia.org/wiki/VCard">vcard</a>.</p>

<p>To supply a ressource as a different content-type, we can use Konstrukts built-in support for content-negotiation, which is an often underutilised feature of the http protocol. To get this, we need to edit the "show" component, so that the <code>GET()</code> method can fork out to either the html or the vcard handler. We could do so manually in the <code>GET()</code> method, but there are generic hooks available for this purpose. After a revision of the component, we end up with this:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="kw2">class</span> components_contacts_Show <span class="kw2">extends</span> k_Component <span class="br0">&#123;</span>
  <span class="kw2">function</span> renderHtml<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw3">global</span> <span class="re0">$contacts</span>;
    <span class="re0">$contact</span> = <span class="re0">$contacts</span>-&gt;<span class="me1">fetchByName</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$contact</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="kw1">throw</span> <span class="kw2">new</span> k_NotFound<span class="br0">&#40;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
    <span class="re0">$this</span>-&gt;<span class="me1">document</span>-&gt;<span class="me1">setTitle</span><span class="br0">&#40;</span><span class="re0">$contact</span>-&gt;<span class="me1">full_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="re0">$t</span> = <span class="kw2">new</span> k_Template<span class="br0">&#40;</span><span class="st0">&quot;templates/contacts-show.tpl.php&quot;</span><span class="br0">&#41;</span>;
    <span class="kw1">return</span> <span class="re0">$t</span>-&gt;<span class="me1">render</span><span class="br0">&#40;</span><span class="re0">$this</span>, <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">'contact'</span> =&gt; <span class="re0">$contact</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  <span class="kw2">function</span> renderVcard<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw3">global</span> <span class="re0">$contacts</span>;
    <span class="re0">$contact</span> = <span class="re0">$contacts</span>-&gt;<span class="me1">fetchByName</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$contact</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="kw1">throw</span> <span class="kw2">new</span> k_NotFound<span class="br0">&#40;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
    <span class="re0">$t</span> = <span class="kw2">new</span> k_Template<span class="br0">&#40;</span><span class="st0">&quot;templates/contacts-show-vcard.tpl.php&quot;</span><span class="br0">&#41;</span>;
    <span class="re0">$response</span> = <span class="kw2">new</span> k_HttpResponse<span class="br0">&#40;</span><span class="br0">&#41;</span>;
    <span class="re0">$response</span>-&gt;<span class="me1">setContent</span><span class="br0">&#40;</span><span class="re0">$t</span>-&gt;<span class="me1">render</span><span class="br0">&#40;</span><span class="re0">$this</span>, <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">'contact'</span> =&gt; <span class="re0">$contact</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="re0">$response</span>-&gt;<span class="me1">setContentType</span><span class="br0">&#40;</span><span class="st0">'text/x-vcard'</span><span class="br0">&#41;</span>;
    <span class="kw1">throw</span> <span class="re0">$response</span>;
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre>

<p>As you can see, we now have two render-methods, instead of a single <code>GET()</code> method. The default <code>GET()</code> method will use these mappings to dispatch to one or the other, using content-type negotiation. There is a default mapping from content-type to renderer name, which you can find in the documentation, or you can override the <code>$renderers</code> property to specify your own; This may also be needed, if you're rendering a content-type that isn't specified amongst the defaults.</p>

<p>Inside of the vcard renderer, we're creating an abnormal response. As discussed above, this circumvents the normal rendering pipeline and allows us fine grained control over the exact http response sent out.</p>

<p>Of course, we need the template to go with it in <code>templates/contacts-show-vcard.tpl.php</code>:</p>

<pre class="php">BEGIN:VCARD
VERSION:<span class="nu0">3.0</span>
N:<span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="re0">$contact</span>-&gt;<span class="me1">first_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="kw2">?&gt;</span>;&lt;?php <span class="kw3">echo</span> <span class="re0">$contact</span>-&gt;<span class="me1">last_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>; <span class="kw2">?&gt;</span>
FN:<span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="re0">$contact</span>-&gt;<span class="me1">full_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>; <span class="kw2">?&gt;</span>
EMAIL;TYPE=PREF,INTERNET:<span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="re0">$contact</span>-&gt;<span class="me1">email</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>; <span class="kw2">?&gt;</span>
REV:<span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="kw3">date</span><span class="br0">&#40;</span><span class="st0">&quot;Y-m-dTH:i:sZ&quot;</span><span class="br0">&#41;</span> . <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>; <span class="kw2">?&gt;</span>
<span class="kw3">END</span>:VCARD</pre>

<p>If you have a browser with the capability of showing the <code>text/x-vcard</code> content-type, you can now request that. Chances are though, that you don't, so to make things a bit easier, there is an alternative. You can specify the content-type after the components name, separated by a semi-colon. Since a content-type contains a slash, it would break the url to include the whole content-type, so you can use the short-name instead. In this case, you can navigate to <code>http://localhost/foo/www/contacts/jabba;vcard</code> which will yield the same result as if you had requested the page with an accept header specifying that it wants <code>text/x-vcard</code> rather than <code>text/html</code>.</p>

<h2 id="component-chain-of-command">Component chain of command</h2>

<p>As you may have noted above, there is some duplication between the two render functions. We can reduce this by moving part of it up into a more general handler on the component. In this case the top-level (<code>dispatch()</code>) would fit:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="kw2">class</span> components_contacts_Show <span class="kw2">extends</span> k_Component <span class="br0">&#123;</span>
  <span class="kw2">protected</span> <span class="re0">$contact</span>;
  <span class="kw2">function</span> dispatch<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw3">global</span> <span class="re0">$contacts</span>;
    <span class="re0">$this</span>-&gt;<span class="me1">contact</span> = <span class="re0">$contacts</span>-&gt;<span class="me1">fetchByName</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">contact</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="kw1">throw</span> <span class="kw2">new</span> k_NotFound<span class="br0">&#40;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
    <span class="kw1">return</span> <span class="kw2">parent</span>::<span class="me2">dispatch</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  <span class="kw2">function</span> renderHtml<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$this</span>-&gt;<span class="me1">document</span>-&gt;<span class="me1">setTitle</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">full_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="re0">$t</span> = <span class="kw2">new</span> k_Template<span class="br0">&#40;</span><span class="st0">&quot;templates/contacts-show.tpl.php&quot;</span><span class="br0">&#41;</span>;
    <span class="kw1">return</span> <span class="re0">$t</span>-&gt;<span class="me1">render</span><span class="br0">&#40;</span><span class="re0">$this</span>, <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">'contact'</span> =&gt; <span class="re0">$this</span>-&gt;<span class="me1">contact</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  <span class="kw2">function</span> renderVcard<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$t</span> = <span class="kw2">new</span> k_Template<span class="br0">&#40;</span><span class="st0">&quot;templates/contacts-show-vcard.tpl.php&quot;</span><span class="br0">&#41;</span>;
    <span class="re0">$response</span> = <span class="kw2">new</span> k_HttpResponse<span class="br0">&#40;</span><span class="br0">&#41;</span>;
    <span class="re0">$response</span>-&gt;<span class="me1">setContent</span><span class="br0">&#40;</span><span class="re0">$t</span>-&gt;<span class="me1">render</span><span class="br0">&#40;</span><span class="re0">$this</span>, <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">'contact'</span> =&gt; <span class="re0">$this</span>-&gt;<span class="me1">contact</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="re0">$response</span>-&gt;<span class="me1">setContentType</span><span class="br0">&#40;</span><span class="st0">'text/x-vcard'</span><span class="br0">&#41;</span>;
    <span class="kw1">throw</span> <span class="re0">$response</span>;
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre>

<p>A component starts its processing in the <code>dispatch()</code> method. This determines if the component is the last handler or it should delegate control to another component. In a nested path, each component will enter the <code>dispatch()</code> method and then delegate to the next, until the last segment of the path is reached.</p>

<p>This component will then delegate to its own <code>execute()</code> method, which again delegates to a method that corresponds to the HTTP method, usually <code>GET()</code>. Finally, the <code>GET()</code> method will try to find a suitable renderer method to dispatch to, based on the content-type.</p>

<p>Each of these functions can be hooked into by overriding them in your components. That allows you to attatch code to run for every renderer (override <code>GET()</code>), for every request-type (override <code>execute()</code>) or even for this and all sub components (override <code>dispatch()</code>). The latter is for example, a good place to put authentication logic. You can also override <code>forward()</code> to affect all sub components, but not it self. This is useful for rendering additional HTML decorations, such as navigation, which could look like this:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="kw2">class</span> components_Root <span class="kw2">extends</span> k_Component <span class="br0">&#123;</span>
  <span class="kw2">function</span> forward<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="st0">'
&lt;div id=&quot;menu&quot;&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;'</span> . <span class="re0">$this</span>-&gt;<span class="me1">url</span><span class="br0">&#40;</span><span class="st0">&quot;/&quot;</span><span class="br0">&#41;</span> . <span class="st0">'&quot;&gt;Back to index&lt;/a&gt;
    &lt;li&gt;&lt;a href=&quot;'</span> . <span class="re0">$this</span>-&gt;<span class="me1">url</span><span class="br0">&#40;</span><span class="st0">&quot;/help&quot;</span><span class="br0">&#41;</span> . <span class="st0">'&quot;&gt;Help&lt;/a&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&quot;content&quot;&gt;
'</span> . <span class="kw2">parent</span>::<span class="me2">forward</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . <span class="st0">'
&lt;/div&gt;'</span>;
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre>

<h2 id="posting-and-forms">Posting and forms</h2>

<p>Most web applications have to do with user input from html-forms. A component can implement a handler for any HTTP method - including the POST, which are closely related to forms. Doing so is just a matter of providing a <code>POST()</code> method on the component. A POST request has some additional properties over a GET request. The payload of the request - usually fields of a form - can be accessed through the <code>body()</code> method. This corresponds to PHPs native <code>$_POST</code> superglobal, just like <code>query()</code> corresponds to <code>$_GET</code>. This naming is more correct, since the querystring is available in all requests - not just GET, and a payload can come with other types of request, such as PUT.</p>

<p>To continue from our running example, the first thing we need is to render a html form for editing a contact. A form can be seen as an alternative rendering of a resource, and for this there is a special content-type reserved. Add the following to out 'show' component:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="kw2">class</span> components_contacts_Show <span class="kw2">extends</span> k_Component <span class="br0">&#123;</span>
  <span class="kw2">function</span> renderEdit<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$this</span>-&gt;<span class="me1">document</span>-&gt;<span class="me1">setTitle</span><span class="br0">&#40;</span><span class="st0">&quot;Edit &quot;</span> . <span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">full_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="re0">$t</span> = <span class="kw2">new</span> k_Template<span class="br0">&#40;</span><span class="st0">&quot;templates/contacts-show-edit.tpl.php&quot;</span><span class="br0">&#41;</span>;
    <span class="kw1">return</span> <span class="re0">$t</span>-&gt;<span class="me1">render</span><span class="br0">&#40;</span><span class="re0">$this</span>, <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">'contact'</span> =&gt; <span class="re0">$this</span>-&gt;<span class="me1">contact</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  ...</pre>

<p>And the template to go with it in <code>templates/contacts-show-edit.tpl.php</code>:</p>

<pre class="php">&lt;h2&gt;Edit <span class="kw2">&lt;?php</span> e<span class="br0">&#40;</span><span class="re0">$contact</span>-&gt;<span class="me1">short_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; ?&gt;&lt;/h2&gt;
&lt;form method=<span class="st0">&quot;post&quot;</span> action=<span class="st0">&quot;&lt;?php e(url()); ?&gt;&quot;</span>&gt;
<span class="kw2">&lt;?php</span> <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re0">$contact</span>-&gt;<span class="me1">errors</span><span class="br0">&#41;</span><span class="br0">&#41;</span>: <span class="kw2">?&gt;</span>
<span class="kw2">&lt;?php</span> <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$contact</span>-&gt;<span class="me1">errors</span> <span class="kw1">as</span> <span class="re0">$error</span><span class="br0">&#41;</span>: <span class="kw2">?&gt;</span>
   &lt;p style=<span class="st0">&quot;color:red&quot;</span>&gt;
     <span class="kw2">&lt;?php</span> e<span class="br0">&#40;</span><span class="re0">$error</span><span class="br0">&#41;</span>; <span class="kw2">?&gt;</span>
   &lt;/p&gt;
<span class="kw2">&lt;?php</span> <span class="kw1">endforeach</span>; <span class="kw2">?&gt;</span>
<span class="kw2">&lt;?php</span> <span class="kw1">endif</span>; <span class="kw2">?&gt;</span>
   &lt;p&gt;
     &lt;label&gt;First Name
       &lt;input type=<span class="st0">&quot;text&quot;</span> name=<span class="st0">&quot;first_name&quot;</span> value=<span class="st0">&quot;&lt;?php e($contact-&gt;first_name()); ?&gt;&quot;</span> /&gt;
     &lt;/label&gt;
  &lt;/p&gt;
   &lt;p&gt;
     &lt;label&gt;Last Name
       &lt;input type=<span class="st0">&quot;text&quot;</span> name=<span class="st0">&quot;last_name&quot;</span> value=<span class="st0">&quot;&lt;?php e($contact-&gt;last_name()); ?&gt;&quot;</span> /&gt;
     &lt;/label&gt;
  &lt;/p&gt;
   &lt;p&gt;
     &lt;label&gt;Emal
       &lt;input type=<span class="st0">&quot;text&quot;</span> name=<span class="st0">&quot;email&quot;</span> value=<span class="st0">&quot;&lt;?php e($contact-&gt;email()); ?&gt;&quot;</span> /&gt;
     &lt;/label&gt;
  &lt;/p&gt;
   &lt;p&gt;
     &lt;input type=<span class="st0">&quot;submit&quot;</span> /&gt;
   &lt;/p&gt;
&lt;/form&gt;</pre>

<p>This is obviously a primitive way to render form components. HTML widgets are outside the scope of Konstrukt, but you can use third party libraries, such as Zend_Form to generate the html. If you use a real template engine, it may also provide a means for this.</p>

<p>Now that we have the rendering of a form in place, we still need to process the POST request it will generate. Time to add yet another method to our component:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="kw2">class</span> components_contacts_Show <span class="kw2">extends</span> k_Component <span class="br0">&#123;</span>
  <span class="kw2">function</span> POST<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">process</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="kw1">throw</span> <span class="kw2">new</span> k_SeeOther<span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">url</span><span class="br0">&#40;</span><span class="st0">';'</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
    <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">GET</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  <span class="kw2">function</span> process<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw3">global</span> <span class="re0">$contacts</span>;
    <span class="re0">$this</span>-&gt;<span class="me1">contact</span> = <span class="kw2">new</span> Contact<span class="br0">&#40;</span>
      <span class="kw3">array</span><span class="br0">&#40;</span>
        <span class="st0">'short_name'</span> =&gt; <span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">short_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span>,
        <span class="st0">'first_name'</span> =&gt; <span class="re0">$this</span>-&gt;<span class="me1">body</span><span class="br0">&#40;</span><span class="st0">&quot;first_name&quot;</span><span class="br0">&#41;</span>,
        <span class="st0">'last_name'</span> =&gt; <span class="re0">$this</span>-&gt;<span class="me1">body</span><span class="br0">&#40;</span><span class="st0">&quot;last_name&quot;</span><span class="br0">&#41;</span>,
        <span class="st0">'email'</span> =&gt; <span class="re0">$this</span>-&gt;<span class="me1">body</span><span class="br0">&#40;</span><span class="st0">&quot;email&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">first_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      @<span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">errors</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="st0">&quot;Missing first_name&quot;</span>;
    <span class="br0">&#125;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">last_name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      @<span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">errors</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="st0">&quot;Missing last_name&quot;</span>;
    <span class="br0">&#125;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">email</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      @<span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">errors</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="st0">&quot;Missing email&quot;</span>;
    <span class="br0">&#125;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="kw3">isset</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">errors</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      try <span class="br0">&#123;</span>
        <span class="re0">$contacts</span>-&gt;<span class="me1">save</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">contact</span><span class="br0">&#41;</span>;
      <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span>Exception <span class="re0">$ex</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        @<span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">errors</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$ex</span>-&gt;<span class="me1">getMessage</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
        <span class="kw1">return</span> <span class="kw2">false</span>;
      <span class="br0">&#125;</span>
      <span class="kw1">return</span> <span class="kw2">true</span>;
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
  ...</pre>

<p>As this tutorial isn't about the model layer, the implementation of <code>process()</code> is kept simple. You can test a failure case by omitting one of the fields. Otherwise the entity is saved back to the database.</p>

<p>The <code>POST()</code> method - while brief - deserves a few words on the way. If the processing goes well, we generate a redirect. In this case it's for the current components url, with a neutral content-type. That will take the user back to the default representation after a successful operation. If there are failures, the form is redisplayed.</p>

<p>This pattern is commonly known as "redirect after post" or "post-redirect-get" (PRG). Doing things in this exact way means that the users browser history won't store the POST actions, once the form has been completed. If we didn't do this, using the "back" button of the browser would try to re-submit the form. You have probably experienced this annoying behaviour on a web site more than once. Note that it is only after the form has been succesfully processed, that the redirect takes place.</p>
</body>
</html>
